package com.view.test.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.view.test.R;
import com.view.test.adapter.ChannelManagerAdapter;
import com.view.test.adapter.ChannelManagerItemTouchHelper;
import com.view.test.model.ChannelModel;

import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * @Author：wangyibo
 * @Date：2021/11/23 10:45
 */
public class ChannelManagerView extends RelativeLayout
        implements View.OnClickListener, ChannelManagerAdapter.OnClickListener {

    private  Context context;
    private TextView edit;
    boolean editStatus;
    private RecyclerView recycle_view;
    private List<ChannelModel> channelModels;
    private ChannelManagerAdapter channelManagerAdapter;
    private boolean disableClick;

    public ChannelManagerView(Context context) {
        super(context);
    }

    public ChannelManagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        infoView(context);
    }

    public void setDatas(List<ChannelModel> channelModels) {
        this.channelModels = channelModels;
        channelManagerAdapter.setDatas(channelModels);
    }

    private void infoView(Context context) {
        View inflate = View.inflate(context, R.layout.channel_manager_layout, null);
        addView(inflate);

        recycle_view = inflate.findViewById(R.id.recycle_view);
        edit = inflate.findViewById(R.id.edit);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        recycle_view.setLayoutManager(gridLayoutManager);
        recycle_view.setItemAnimator(new SlideInLeftAnimator());


        channelManagerAdapter = new ChannelManagerAdapter(context);
        recycle_view.setAdapter(channelManagerAdapter);
        channelManagerAdapter.setOnLongListener(this);


        edit.setOnClickListener(this);
    }

    public void disableLongClick(boolean disableClick){
        this.disableClick = disableClick;
        if (!disableClick){
            ItemTouchHelper helper = new ItemTouchHelper(new ChannelManagerItemTouchHelper(channelManagerAdapter, context));
            helper.attachToRecyclerView(recycle_view);
        }else {
            edit.setVisibility(View.GONE);
        }
        channelManagerAdapter.disableClick(disableClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit:
                if (editStatus) {
                    editStatus = false;
                    edit.setText("编辑");
                } else {
                    editStatus = true;
                    edit.setText("完成");
                }
                channelManagerAdapter.setEditStatus(editStatus);
                break;
        }
    }

    public interface OnClickCallBack {
        void onClick(View v, int position, ChannelModel channelModel);
    }

    private OnClickCallBack onListener;

    public void setOnClickCallBack(OnClickCallBack longListener) {
        this.onListener = longListener;
    }


    @Override
    public void onLongClick(View v, int position) {
        editStatus = true;
        edit.setText("完成");
    }

    @Override
    public void onClick(View v, int position, ChannelModel channelModel) {
        if (onListener != null){
            onListener.onClick(v,position,channelModel);
        }
    }

}
